//
//  ResetPassView3.m
//  specialine
//
//  Created by 往返网络 on 17/2/10.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ResetPassView3.h"

#define RETURN_BTN 1000  //上级界面

#define NEXT_View  2000 //下一页

@interface ResetPassView3 ()<UITextFieldDelegate>

@end

@implementation ResetPassView3
{
    
    UITextField * newpasstf;
    
    UITextField * againnewpasstf;
    
    UIButton * nextbtn;
    
    
}

- (instancetype)initWithFrame:(CGRect)frame{


    if (self = [super initWithFrame:frame]) {
        
        [self createUI];
        
    }


    return self;
    

}

- (void)createUI{

    //适配字体大小,圆角大小
    
    float fontsie = 15;
    
    float cornersize = 20;
    
    fontsie = SCREEN_H < 667 ? 13 :  17;
    
    cornersize = SCREEN_H < 667 ? 18 : 22 ;
    
    
    if (SCREEN_H == 667) {
        
        fontsie = 15;
        
        cornersize = 20;
        
    }
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 80.0/W_BASE*SCREEN_W, 44)];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIFont * iconfont = [UIFont fontWithName:@"iconfont" size:fontsie+2];
    
    btn.titleLabel.font = iconfont;
    
    [btn setTitle:@"\U0000e697" forState:UIControlStateNormal];
    
    [self addSubview:btn];
    
    btn.tag = RETURN_BTN;
    
    [btn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //手机号。。密码 输入框创建
    
    newpasstf = [[UITextField alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, 64, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    newpasstf.layer.borderWidth = 1;
    
    newpasstf.font = [UIFont systemFontOfSize:fontsie];
    
    
    //设置tf的缩紧，靠右缩紧，给tf加一个leftview
    
    UIView * leftview  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    newpasstf.leftView = leftview;
    
    newpasstf.leftViewMode = UITextFieldViewModeAlways;
    
    newpasstf.layer.borderColor = [[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1]CGColor];
    
    newpasstf.placeholder = @"新密码";
    
    newpasstf.clearButtonMode = UITextFieldViewModeAlways;
    
    newpasstf.layer.cornerRadius = cornersize;
    
    [self addSubview:newpasstf];
    
    
    againnewpasstf = [[UITextField alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(newpasstf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    
    againnewpasstf.layer.borderWidth = 1;
    
    UIView * leftview1  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    againnewpasstf.leftView = leftview1;
    
    againnewpasstf.font = [UIFont systemFontOfSize:fontsie];
    
    againnewpasstf.leftViewMode = UITextFieldViewModeAlways;
    
    againnewpasstf.layer.borderColor = [[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1]CGColor];
    
    againnewpasstf.placeholder = @"再次输入新密码";
    
    againnewpasstf.clearButtonMode = UITextFieldViewModeAlways;
    
    
    againnewpasstf.layer.cornerRadius = cornersize;
    
    [self addSubview:againnewpasstf];
    
    
    newpasstf.delegate = self;
    
    againnewpasstf.delegate = self;
    
    newpasstf.secureTextEntry = YES;
    
    againnewpasstf.secureTextEntry = YES;
    
    
    
    nextbtn = [[UIButton alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(againnewpasstf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    
    nextbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:0.5];
    
    [nextbtn setTitle:@"下一步" forState:UIControlStateNormal];
    
    nextbtn.layer.cornerRadius = cornersize;
    
    nextbtn.titleLabel.font = [UIFont systemFontOfSize:fontsie];
    
    nextbtn.userInteractionEnabled = NO;
    
    [self addSubview:nextbtn];
    
    nextbtn.tag = NEXT_View;
    
    [nextbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    

}


- (void)enterClick:(UIButton *)btn{

    NSInteger btn_tag = btn.tag;
    
    switch (btn_tag) {
            
        case RETURN_BTN:
            //返回登陆页面
            
            [self.reset3delegate enterResetPassVC2BtnType:ResetPassView3BtnClickReturn andFisrtstr:nil andsecondstr:nil];
            
            
            break;
            
        case NEXT_View:
            //进入主页
            
            if ([newpasstf.text isEqualToString:againnewpasstf.text]){
                
              //  NSLog(@"same");
                
                [self.reset3delegate enterResetPassVC2BtnType:ResetPassView3BtnClickNextBtn andFisrtstr:newpasstf.text andsecondstr:againnewpasstf.text];
                

            }else{
                
             //   NSLog(@"different");
                
                ALERT(@"提示", @"密码输入不一致", nil, @"确认", nil);
                
                
            }
            
            
            break;

        default:
            
            break;
            
            
    }

    

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    
    
    NSString * teststring = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (teststring.length >= 1) {
        
        nextbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
        
        nextbtn.userInteractionEnabled = YES;
    }

    
//    NSString * textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
//    
//    
//    //2次密码都为6位时，变色，下一步按钮可用
//    if ((textstring.length >= 6 && textField == newpasstf) && (againnewpasstf.text.length == 6)) {
//        
//        nextbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
//        
//        nextbtn.userInteractionEnabled = YES;
//        
//        
//    }
//    else if ((textstring.length >= 6 && textField == againnewpasstf) && (newpasstf.text.length == 6)){
//        
//        
//        nextbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
//        
//        nextbtn.userInteractionEnabled = YES;
//        
//        
//    }
    
    return YES;
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{


    [self endEditing:YES];
    
    return YES;


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [newpasstf resignFirstResponder];
    
    [againnewpasstf resignFirstResponder];
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
